package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JOptionPane;

import model.Employee;
import model.Order;

public class OrderTopDAO {
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement pst = null;

	// constructor
	public OrderTopDAO() {
		conn = DatabaseConnect.ConnectDb();
		System.out.println("OrderTopDAO contructor in is success!");
	}

	public ResultSet getOrderTopResultSet() {
		rs = null;
		String sql = "select * from Orders";
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
		
			return rs;
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(null, "OrderTopDAO empty resultset");
		}
		return rs;
	}

	public void newOrderTop(int ssn) {
		String sql = "insert into Orders (SSN, TotalPrice) values (?,?)";
		
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1,ssn);
			pst.setDouble(2, 0.0);
			
			pst.executeUpdate();
			pst.close();
		} catch (Exception e) {
			System.out.println("OrderTopDao, new OrderTOp exception!");
		}
	}
	
	
	
	public void deleteOrderTop(String orderId) {
		String sql = "delete from Orders where OrderId=?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, Integer.parseInt(orderId));
			pst.executeUpdate();
			pst.close();
		} catch (Exception e) {
			System.out.println("OrderTopDao, deleteOrderTop exception!");
		}
		
	}

	public Order getOrderTop(int orderId) {
		// Open Connection; Convert row to domain object; return order object; return new Order
		rs = null;
		Order result = null;
		String sql = "select * from Orders where OrderId=?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, orderId);
			rs = pst.executeQuery();

			if (rs.next()) {
				result = new Order(rs.getInt("OrderId"), rs.getInt("SSN"),
								rs.getDate("Date"), rs.getDate("Time"),
								rs.getDouble("TotalPrice") );
			}

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "LoginDao fault!!");
			return result;
		}
		return result;
	}

	
}
